// 权限控制
import router from './router'
import store from './store'
import { getToken } from './utils/storage'

const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  const token = getToken()
  // 判断
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.userInfo?.userId) {
        store.dispatch('login/getIUserInfo')
      }
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
